## -*- coding: utf-8 -*-
<%namespace file="/component/utils.html" import="*" />

<%!
    from pythonblog.service import section_service, content_service, user_service
%>

<%def name="display_first_post(post, section_id)">
    %if post:
    <div>
    ${first_post(post, section_id)}
    </div>
    %endif
</%def>

<%def name="display_posts(posts)">
  <div style="padding: 0 5px 0 5px">
    %for post in posts:
    <div>
      ${common_post(post)}
    </div>
    %endfor
  </div>
</%def>

<%def name="display_news(news)">
    %for index, post in ((i, news[i]) for i in range(len(news))):
    <% user = user_service.get_user_by_id(post.user_id) %>
    <div>
      <div class="blog">
          <div class="content" style="padding-top:5px;padding-right:5px;">
            <div id="right"> 文 / ${link_for_user(user.userName, user.author)}</div>
            <div>${post_title(post)}</div>
          </div>
      </div>
    </div>
    %endfor
</%def>

<%def name="display_top_posts(posts)">    
    %for post in posts:
    <% user = user_service.get_user_by_id(post.user_id) %>
    <li><span style="padding:3px;" title="浏览次数:${post.viewTimes}, 评论:${post.comment_count}">${'%s + %s' % (post.viewTimes, post.comment_count)}</span> ${post_title(post, 15)} 文 / ${link_for_user(user.userName, user.author)}</li>
    %endfor
</%def>

<%def name="display_new_posts(posts)">
    %for post in posts:
    <li><span style="padding:3px;" title="创建时间:${post.createDate}">${post.createDate_only}</span> ${post_title(post, 15)}</li>
    %endfor
</%def>

<%def name="first_post(post, image_index)">
  <p class="firstpost"> 
  <img src="${h.url_for('/images/sub_section_%s.jpg'%image_index)}" class="firstpost" />
  <div style="font-weight:bold;font-size:110%;">${post_title(post, 12)}</div>
  <div style="text-align:right;"> 文 / ${link_for_user(post.user.userName, post.user.author)}</div><br>
  ${post.abstract} ${read_post(post.id)}<br>
  <div class="blog_info" style="text-align:right;padding-right:5px;">${_post_info(post)}</div>
  </p>
</%def>

<%def name="read_post(post_id)">
[<a href="${h.url_for_www(controller='post', action='view', id=post_id)}">阅读全文</a>]
</%def>

<%def name="common_post(post)">
<div class="blog">
  <div class="content">
    ${post_title_with_auth(post, 20)}
    ${post_info(post)}
  </div>
</div>
</%def>

<%def name="long_first_post(section_id)">
  <% post = content_service.get_section_first_post(section_id) %>
  %if post:
  <div style="height: 120px;">
      <p class="firstpost">
      <img src="${h.url_for('/images/sub_section_%s.jpg'%section_id)}" class="firstpost" />
      <div><div id="right"> 文 / ${link_for_user(post.user.userName, post.user.author)}</div><div style="font-weight:bold;font-size:110%;">${post_title(post, 20)}</div></div>
      <div class="blog_info" style="text-align:right;background-color:#FFFFFF;padding-right: 5px;">${_post_info(post)}</div>
      ${post.abstract} ${read_post(post.id)}
      </p>
  </div>
  %endif
</%def>

<%def name="post_title_with_auth(post, count=15)">
<div style="background-color:#F2F8EF;">${post_title(post, count)}</div>
</%def>

<%def name="post_title(post, count=18)">
${link_for_post(post, 'www', count)}
</%def>

<%def name="post_info(post)">
<div id="right"> 文 / ${link_for_user(post.user.userName, post.user.author)}</div>
<div class="blog_info">${_post_info(post)}</div>
</%def>

<%def name="_post_info(post)">
<span class="comment">${link_for_comment(post.id, u'评论(%s)'%post.comments_count)}</span> <span class="view">浏览(${post.viewTimes})</span>
</%def>

<%def name="post_info_full(post)">
<span class="comment">${link_for_comment(post.id, u'评论(%s)'%post.comments_count)}</span> <span class="view">浏览(${post.viewTimes})</span> <span class="tag">${post.tags_display}</span> <span class="date">${post.updateDate}</span>
</%def>

<%def name="comment_show(comment)">
<div>
  <div class="comment_title">
      <div style="float:right;">${comment.createDate}</div>
      <div>
      % if comment.user:
      ${link_for_user(comment.user.userName, comment.user.author)}
      % else:
      匿名
      % endif
      </div>
  </div>
  <div class="comment_content">
  ${h.literal(comment.content)}
  </div>
</div>
</%def>

<%def name="display_comments(comments)">
    <h5>评论</h5>
    <a name="comments" id="comments"></a>
    % if len(comments):
        % for comment in comments:
            ${comment_show(comment)}
        % endfor

        % if comments.last_page > 1:
            ${paginate(comments, request.environ.get('PATH_INFO'), '#comments')}
        % endif
    % endif
</%def>

<%def name="blog_comments()">
    <h5>发表评论</h5>
    % if not h.auth.authorized(h.auth.is_valid_user):
    <span style="color: red;">您尚未登录，只能匿名发表评论！<a href="${h.url_for_www(controller='account', action='register')}">点击这里注册</a></span>
    % endif
    ${h.form('', method='post', id='comment_form')}
    <table width="100%">
      <tbody>
        <tr><td id="commentErrorWrapper"></td></tr>
        <tr>
          <td id="editWrapper">${h.textarea("content", "", style='height:250px;width:600px;')}</td>
        </tr>
        <tr>
          <td style="padding-top:10px;"> 提示：内容长度最大5千汉字，如果出现超长，请分开发布 </td>
        </tr>
        <tr>
          <td style="padding:15px 0px 25px 0px"><input id="btnSubmit" class="submit" type="button" value="　发表　" /></td>
        </tr>
      </tbody>
    </table>
    ${h.end_form()}
</%def>

<%def name="blog_info(post, author=False, extra=None, hint=None)">
<span class="comment">${link_for_comment(post.id, u'评论: %s条'%post.comments_count)}</span>
<span class="view">浏览: ${post.viewTimes}次</span>
<span class="tag">标签: ${post.tags_display}</span>
% if author:
<span class="author">${link_for_user(c.post.user.userName, u'作者: %s'%c.post.user.author)}</span>
% endif

% if extra:
<span class="hint" title="${hint}">${h.literal(extra)}</span>
% else:
<span class="date">${h.get_readable_date(post.createDate)}</span>
% endif
</%def>

<%def name="blog_show(post, show_full=False)">
% if show_full:
<div>
    <div style="float: right;">
        ${digg(post)}
    </div>
    <div class="blog_title">
        <div>${c.post.updateDate}</div>
        <h3>${link_for_post(c.post, c.userInfo.userName)}</h3>
        ${flash()}
        <strong>标签: ${c.post.tags_display}</strong>
    </div>
</div>
% else:
<div class="blog_title">
  <div>${post.updateDate}</div>
  % if h.auth.authorized(h.auth.is_valid_user) and c.userInfo.userName == request.environ['REMOTE_USER']:
    <div class="show_full_flag"><a class="favorite" href="${h.url_for(controller='post', action='edit', id=post.id, sub_domain=c.userInfo.userName)}">修改</a></div>
  % endif
  <h3>${link_for_post(post, c.userInfo.userName)}</h3>
  ${flash()}
  <strong>标签: ${post.tags_display}</strong>
</div>
% endif
<div class="blog_content"> ${h.literal(show_full and post.content or post.abstract)} </div>
% if show_full:
<div id="dig_bottom">
    ${digg_bottom(c.post)}
</div>
% endif
<div class="blog_bottom">
  <ul>
    <li>浏览 (${post.viewTimes})</li>
    <li>${link_for_comment(post.id, u'评论', c.userInfo.userName)} (${post.comments_count})</li>
    <li class="last">分类: ${link_for_section(section_service.get_section(post.section_id))}</li>
  </ul>
</div>
</%def>

<%def name="draft_show(post)">
<tr id='${"draft%s" % post.id}'>
  <td><a title="${post.title}" href="${h.url_for(controller='post', action='edit_draft', id=post.id, sub_domain=c.userInfo.userName)}">${post.title}</a></td>
  <td>${post.createDate}</td>
  <td>${post.updateDate}</td>
  <% section = section_service.get_section(post.section_id) %>
  <td>${(not section) and u'未分类' or section.name}</td>
  <% del_url = h.url_for(controller='post', action='delete', id=post.id, sub_domain=c.userInfo.userName) %>
  <td><a title="编辑" href="${h.url_for(controller='post', action='edit_draft', id=post.id, sub_domain=c.userInfo.userName)}"><img src="/images/blog/btn_edit.png" alt="编辑"/></a>  <a href="#"><img onclick="if (confirm('你确定要删除这篇文章？')) { window.location='${del_url}'; }" style="cursor:hand;" src="/images/blog/btn_delete.png" title="删除" alt="删除"/></a> </td>
</tr>
</%def>